package demo.utils;

import com.alibaba.druid.pool.DruidDataSource;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class DataSourceUtils {
    private static DataSource dataSource;
    static {
        /*//最初
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://127.0.0.1/db1");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("123456");
        dataSource=druidDataSource;*/
       DruidDataSource druidDataSource = new DruidDataSource();
        try {
            Properties properties=new Properties();
            InputStream in = DataSourceUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
            properties.load(in);
            druidDataSource.setDriverClassName(properties.getProperty("jdbc.driverClassName"));
            druidDataSource.setUrl(properties.getProperty("jdbc.url"));
            druidDataSource.setUsername(properties.getProperty("jdbc.username"));
            druidDataSource.setPassword(properties.getProperty("jdbc.password"));
            dataSource=druidDataSource;
        } catch (IOException e) {
            e.printStackTrace();
        }

        //ResourceBundle resourceBundle=ResourceBundle.getBundle("jdbc");

    }

    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

}
